//app.js
App({
  onLaunch: function () {
    let that = this;
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    that.globalData.appid = extConfig.appid
    that.ihttpGet('https://www.58zltc.com/api.php/Index/getShopid/appid/' + that.globalData.appid, function (res) {
      
      if (res.data.code == 0) {
        that.globalData.shop_id = res.data.shop_id
        that.getUserInfo();
      } else {
        wx.showModal({
          title: '警告',
          content: res.data.msg,
        })
        return false;
      }
    })
    wx.getStorage({
      key: 'openid',
      success: function(res) {
        if (res.data != undefined){
          that.globalData.openid = res.data;
        }else{
          that.getOpenid();
        }
      },
      fail: function(res){
        that.getOpenid();
      }
    })
   
  },
  getUserInfo: function(){
    // 获取用户信息
    let that = this;
    wx.getUserInfo({
      success: res => {
        // 可以将 res 发送给后台解码出 unionId
        let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
        res.userInfo.openid = that.globalData.openid;

        that.ihttpPost('https://www.58zltc.com/api.php/Index/user', res.userInfo, function(res){
          console.log(res)
        })
        that.globalData.userInfo = res.userInfo
      },
      fail: function (res) {
        that.showModal()
      }
    })
  },

  showModal: function () {
    let that = this;
    wx.showModal({
      title: '提示',
      content: '请允许微信授权,使用我的用户信息.',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.openSetting({
            success: function(data) {
              if(data) {
                if (data.authSetting["scope.userInfo"] == true) {
                  loginStatus = true;
                  wx.getUserInfo({
                    withCredentials: false,
                    success: function(data) {
                      console.info("2成功获取用户返回数据");
                      console.info(data.userInfo);
                    },
                    fail: function() {
                      console.info("2授权失败返回数据");
                    }
                  });
                }
              }
            },
            fail: function() {
              console.info("设置失败返回数据");
            }
          });
        } else if (res.cancel) {
          that.showModal()
        }
      }
    })
  },

  getUserInfo1: function (cb) {
    var that = this
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口
      wx.login({
        success: function () {
          wx.getUserInfo({
            success: function (res) {
              that.globalData.userInfo = res.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      })
    }
  },
  getOpenid: function(){
    let that = this;
    wx.login({
      success: res => {
        that.ihttpPost("https://www.58zltc.com/api.php/Index/login",{code: res.code},
        function(res){
          if (res.data.err_code > 0) {
            // that.getOpenid();
          } else {
            wx.setStorage({
              key: 'openid',
              data: res.data.openid,
            })
            that.globalData.openid = res.data.openid;
          }
        });
      }, fail: function (res) {
        console.log(res)
      }
    }
    )
  },
  ihttpPost: function (url, postData, doSuccess, doFail){
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    const token = extConfig.appid;
    wx.request({
      url: url + '?token=' + token,
      method: 'POST',
      data: postData,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res=>{
        doSuccess(res);
      }
    })
  },
  ihttpGet: function (url, doSuccess, doFail) {
    wx.showLoading({
      title: '加载中…',
    })
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    const token = extConfig.appid;
    wx.request({
      url: url + '?token=' + token,
      success: res => {
        doSuccess(res);
        wx.hideLoading();
      }
    })
  },
  iModel: function(content){
    wx.showModal({
      title: '提示',
      content: content,
      showCancel: false,
      confirmText: '我知道了'
    })
  },
  addFormId: function(formId){
    this.ihttpPost("https://www.58zltc.com/api.php/Index/addFormId", { form_id: formId, openid: this.globalData.openid}, function(res){});
  },
  globalData: {
    shop_id:null,
    userInfo: null,
    openid: null,
  }
})